﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace JEdwonTune.Lib {
    public class IISHelper {
        public const String ROOTPATH = "IIS://localhost/W3SVC/1/ROOT";
        public const String DIRTYPENAME = "IIsWebVirtualDir";
        /// <summary>
        /// 判断虚拟目录是否存在
        /// </summary>
        /// <param name="virtualDirName"></param>
        /// <returns></returns>
        public static Boolean CheckVirtualDirExists(String virtualDirName) {
            DirectoryEntry rootfolder = new DirectoryEntry(ROOTPATH);
            DirectoryEntries des = rootfolder.Children;
            try {
                DirectoryEntry de = des.Find(virtualDirName, DIRTYPENAME);
                return (de != null);
            } catch {
                return false;
            }
        }
        /// <summary>
        /// 活的虚拟目录的实际物理路径
        /// </summary>
        /// <param name="virtualDirName"></param>
        /// <returns></returns>
        public static String GetVirtualDirPath(String virtualDirName) {
            try {
                DirectoryEntry rootfolder = new DirectoryEntry(ROOTPATH);
                DirectoryEntries des = rootfolder.Children;
                DirectoryEntry de = des.Find(virtualDirName, DIRTYPENAME);
                return de.Properties["Path"].Value.ToString();
            } catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// 发布一个虚拟目录
        /// </summary>
        /// <param name="virtualDirName"></param>
        /// <param name="folder"></param>
        public static void PublishVirtualDir(String virtualDirName, String folder ,Boolean cover) {
            DirectoryEntry rootfolder = new DirectoryEntry(ROOTPATH);
            DirectoryEntry de ;
            try {
                de = rootfolder.Children.Find(virtualDirName, DIRTYPENAME);
            } catch { de = null; }
            try {
                if (de != null) {
                    if (cover) {
                        de.Invoke("AppDelete", null);
                        rootfolder.Children.Remove(de);
                        rootfolder.CommitChanges();
                    } else {
                        return;
                    }
                }
                DirectoryEntry newVirDir = rootfolder.Children.Add(virtualDirName, DIRTYPENAME);
                newVirDir.Properties["Path"].Value = folder;
                newVirDir.Invoke("AppCreate", true);
                newVirDir.CommitChanges();
                rootfolder.CommitChanges();
            } catch (Exception ex) {
                throw ex;
            }
        }
        /// <summary>
        /// 删除一个虚拟目录
        /// </summary>
        /// <param name="virtualDirName"></param>
        public static void RemoveVirtualDir(String virtualDirName) {
            try {
                DirectoryEntry rootfolder = new DirectoryEntry(ROOTPATH);
                DirectoryEntry de = rootfolder.Children.Find(virtualDirName, DIRTYPENAME);
                de.Invoke("AppDelete", null);
                rootfolder.CommitChanges();
            } catch (Exception ex) {
                throw ex;
            }
        }
    }
}
